home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10380 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  72 lines

  1. Path: news.magic.mb.ca!usenet
  2. From: jranta@astral.magic.ca (jranta)
  3. Newsgroups: comp.lang.c++
  4. Subject: Newbie question re: struct pointers & member access
  5. Date: 7 Mar 1996 15:56:17 GMT
  6. Organization: Magic Online Services Winnipeg.
  7. Message-ID: <4hn0v2$7q5@aahz.magic.mb.ca>
  8. NNTP-Posting-Host: baator21.magic.ca
  9. X-Newsreader: WinVN 0.92.6
  10.  
  11. Hi!
  12. I am attempting to write a simple program to prepare myself for a C++ course
  13. at school.  The program is to request from the user how many cars to catalog
  14. and to then record this many cars.  I'm using the "new" operator to allocate
  15. the proper number of structures; there doesn't seem to be a problem with input
  16. but when I attempt to ouput using the pointer, all I get is NULL for each
  17. record.
  18.  
  19. Here is the code so far:
  20.  
  21. #include<iostream.h>
  22. #include<conio.h>
  23.  
  24. const int ArSize = 30;
  25. void enter(int t);
  26.  
  27. struct car
  28.     {
  29.      char make[ArSize];
  30.      int year;
  31.     };
  32. int main(void)
  33. {
  34.  int total;
  35.  clrscr();
  36.  cout<<"How many cars do you wish to catalog?";
  37.  cin>>total;
  38.  enter(total);
  39.  
  40. return 0;
  41. }
  42. void enter(int t)
  43. {
  44.  car *ptr = new car[t];
  45.  for(int a=0;a<t;a++,ptr++)
  46.     {
  47.         cin.get(); //to clear input queue(alt. between strings & numbers).
  48.         cout<<"Car #"<<a+1;
  49.     cout<<"\nPlease enter make: ";     
  50.     cin.getline(ptr->make,ArSize);
  51.     cout<<"\nEnter year: ";
  52.     cin>>ptr->year;
  53.     } 
  54. return;
  55. }
  56.  
  57. I attempted to use another "for" loop to display output, but I think that I'm
  58. not setting the pointer properly at the first record.  This is where my 
  59. confusion lies: how do I set the pointer at the beginning and then increment
  60. it?  My output was all NULL, as mentioned above, so I guess that I was 
  61. displaying from the last pointer position and on, resulting in garbage.
  62.  
  63. Any help/hints would be appreciated (I know this concept is *very* important
  64. for OOP, which is what my course is.).
  65.  
  66. If preferred and/or convenient, e-mail me at:
  67.  
  68. jranta@astral.magic.ca
  69.  
  70. Thanks a lot in advance,
  71. Jouni
  72.